package com.colin.app.activity.senior.touch;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.colin.library.help.LogHelp;
import com.colin.library.view.help.MetricsHelp;

/**
 * Created by Colin on 2017/3/2.
 */

public class TouchViewGroupB extends ViewGroup {
    private Context context ;
    public TouchViewGroupB(Context context) {
        this(context, null, 0);
    }

    public TouchViewGroupB(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TouchViewGroupB(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context ;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        LogHelp.d("TouchView", "ViewGroupB onTouchEvent----------->>" + event.getAction());
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        LogHelp.d("TouchView", "ViewGroupB dispatchTouchEvent----->>" + event.getAction());
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        LogHelp.d("TouchView", "ViewGroupB onInterceptTouchEvent-->>" + event.getAction());
        return super.onInterceptTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 计算出所有的childView的宽和高
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        //获得此ViewGroup上级容器为其推荐的宽和高，以及计算模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        int viewHeight = MeasureSpec.getSize(heightMeasureSpec);
        //遍历每个子View
        int childrenWidth = 0;
        int childrenHeight = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            // 测量每一个child的宽和高
            measureChild(child, widthMeasureSpec, widthMeasureSpec);
            //调用子View的onMeasure，设置他们的大小。childWidthMeasureSpec ， childHeightMeasureSpec ?
            MarginLayoutParams marginLayoutParams = (MarginLayoutParams) child.getLayoutParams();
            childrenWidth = child.getMeasuredWidth();
            childrenHeight = child.getMeasuredHeight();
            childrenWidth += marginLayoutParams.leftMargin + marginLayoutParams.rightMargin;
            childrenHeight += marginLayoutParams.topMargin + marginLayoutParams.bottomMargin;
        }
        int defaultDimension = MetricsHelp.dp2px(context, 100);
        //得到最大值
        defaultDimension = Math.max(defaultDimension,childrenWidth);
        defaultDimension = Math.max(defaultDimension,childrenHeight);
        //处理 wrap_content问题
        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(defaultDimension, defaultDimension);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(defaultDimension, viewHeight);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(viewWidth, defaultDimension);
        }
    }

    @Override
    protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        super.measureChildren(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
        super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int parentLeft = getPaddingLeft();
        final int parentTop = getPaddingTop();
        final int parentRight = r - l - getPaddingRight();
        final int parentBottom = b - t - getPaddingBottom();

        final int viewWidth = getMeasuredWidth();
        final int viewHeight = getMeasuredHeight();
        LogHelp.d("viewWidth = " + viewWidth + ", viewHeight = " + viewHeight );
        int left = 0;
        int top = 0;
        int right = 0;
        int bottom = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            if (childView.getVisibility() != GONE) {
                final int childViewWidth = childView.getMeasuredWidth();
                final int childViewHeight = childView.getMeasuredHeight();
                LogHelp.d("childViewWidth = " + childViewWidth + ", childViewHeight = " + childViewHeight );
                MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childView.getLayoutParams();
                //居中处理
                left = Math.max(parentLeft + marginLayoutParams.leftMargin, (viewWidth - childViewWidth) / 2);
                top = Math.max(parentTop + marginLayoutParams.topMargin, (viewHeight - childViewHeight) / 2);

                right = Math.min(parentRight + marginLayoutParams.rightMargin, viewWidth - ((viewWidth - childViewWidth) / 2));
                bottom = Math.min(parentBottom + marginLayoutParams.bottomMargin, viewHeight - ((viewHeight - childViewHeight) / 2));
                childView.layout(left, top, right, bottom);
            }
        }
    }

    /**
     * 停止线程或动画
     */
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
}
